{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  林子涵\n",
    "+ 学号  0165168"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这一刻你是一个最忧愁的人\n",
      "你有著多少温柔\n",
      "才能从不轻言伤心\n",
      "而你告别所有对幸福的定义\n",
      "投身万物中 神的爱恨与空虚\n",
      "和你一起 只与你一起\n",
      "玫瑰色的你\n",
      "你是我生命中最壮丽的记忆\n",
      "我会记得这年代里你做的事情\n",
      "你在曾经不仅是你自己\n",
      "你栽出千万花的一生\n",
      "四季中径自盛放也凋零\n",
      "你走出千万人群独行\n",
      "往柳暗花明山穷水尽去\n",
      "玫瑰色的你\n",
      "玫瑰色的你\n",
      "让我日夜地唱吧\n",
      "我深爱着你\n",
      "玫瑰色的你\n",
      "玫瑰色的你\n",
      "玫瑰色的你\n",
      "玫瑰色的你\n",
      "玫瑰色的你\n"
     ]
    }
   ],
   "source": [
    "def fuge():\n",
    "    print(\"玫瑰色的你\" )\n",
    "print(\"这一刻你是一个最忧愁的人\\n你有著多少温柔\\n才能从不轻言伤心\\n而你告别所有对幸福的定义\\n投身万物中 神的爱恨与空虚\\n和你一起 只与你一起\")\n",
    "fuge()\n",
    "print(\"你是我生命中最壮丽的记忆\\n我会记得这年代里你做的事情\\n你在曾经不仅是你自己\\n你栽出千万花的一生\\n四季中径自盛放也凋零\\n你走出千万人群独行\\n往柳暗花明山穷水尽去\")\n",
    "fuge()\n",
    "fuge()\n",
    "print(\"让我日夜地唱吧\\n我深爱着你\")\n",
    "fuge()\n",
    "fuge()\n",
    "fuge()\n",
    "fuge()\n",
    "fuge()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第一种方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共有11个数\n",
      "均值是:6.0\n",
      "样本方差是:11.0\n",
      "一共有13个数\n",
      "均值是:6.923076923076923\n",
      "样本方差是:26.410256410256405\n"
     ]
    }
   ],
   "source": [
    "def statistics( *arg):\n",
    "    count=0\n",
    "    sum=0\n",
    "    for value in arg:\n",
    "        count+=1\n",
    "        sum+=value\n",
    "    ave=sum/count\n",
    "    print(\"一共有%s个数\" % count)\n",
    "    print(\"均值是:%s\" % ave)\n",
    "    sumsquare=0\n",
    "    for value in arg:\n",
    "        sumsquare+=value**2\n",
    "    var=sumsquare/count-ave**2\n",
    "    samplevar=var*count/(count-1)#将有偏的方差修正成无偏方差#\n",
    "    print(\"样本方差是:%s\" % samplevar)\n",
    "statistics(1,2,3,4,5,6,7,8,9,10,11)\n",
    "statistics(2,3,5,9,7,13,1,4,0,8,8,17,13)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第二种方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入数字：1\n",
      "请输入数字：2\n",
      "请输入数字：3\n",
      "请输入数字：a\n",
      "一共有3个数\n",
      "均值是:2.0\n",
      "样本方差是：1.0000000000000004\n"
     ]
    }
   ],
   "source": [
    "def statistics():\n",
    "    count=0\n",
    "    sum=0\n",
    "    sumsquare=0\n",
    "    ave=0\n",
    "    var=0\n",
    "    numbsqua=0\n",
    "    fnumb=0\n",
    "    while 1:\n",
    "        number=input(\"请输入数字：\")\n",
    "        if number.isdigit():\n",
    "            fnumb=float(number)\n",
    "            numbsqua=fnumb**2\n",
    "            sum=sum+float(number)\n",
    "            sumsquare+=numbsqua\n",
    "            count+=1 \n",
    "        else:\n",
    "            break\n",
    "    ave=sum/count\n",
    "    var=sumsquare/count-ave**2\n",
    "    varadj=var*count/(count-1)\n",
    "    print(\"一共有%s个数\" % count)\n",
    "    print(\"均值是:%s\" % ave)\n",
    "    print(\"样本方差是：%s\" % varadj)\n",
    "statistics()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共有11个数\n",
      "均值是:6.0\n",
      "样本标准差是：3.3166247903554\n",
      "一共有12个数\n",
      "均值是:7.333333333333333\n",
      "样本方差是:26.424242424242433\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "#如果要求标准差输入0，要求方差输入非零数即可#\n",
    "def statistics(var_or_stdvar=0,*arg):\n",
    "    count=0\n",
    "    sum=0\n",
    "    for value in arg:\n",
    "        count+=1\n",
    "        sum+=value\n",
    "    ave=sum/count\n",
    "    print(\"一共有%s个数\" % count)\n",
    "    print(\"均值是:%s\" % ave)\n",
    "    sumsquare=0\n",
    "    for value in arg:\n",
    "        sumsquare+=value**2\n",
    "    var=sumsquare/count-ave**2\n",
    "    samplevar=var*count/(count-1)#将有偏的方差修正成无偏方差#\n",
    "    if var_or_stdvar:\n",
    "        print(\"样本方差是:%s\" % samplevar)\n",
    "    else:\n",
    "        varstd=math.sqrt(samplevar)\n",
    "        print(\"样本标准差是：%s\" %varstd )\n",
    "statistics(0,1,2,3,4,5,6,7,8,9,10,11)\n",
    "statistics(2,3,5,9,7,13,1,4,0,8,8,17,13)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "样本中最大值是：5\n",
      "样本中最小值是：2\n",
      "均值标准误为：0.7602631123499272\n",
      "\n",
      "\n",
      "样本中最大值是：11\n",
      "样本中最小值是：5\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "#如果要求均值标准误，最大值，最小值请输入非0的数，不输出请输入0即可#\n",
    "def statistics(meanerror=None,max=None,min=None,*arg):\n",
    "    count=0\n",
    "    sum=0\n",
    "    nsquare=0\n",
    "    if max:\n",
    "        for value in arg:\n",
    "            if value>max:\n",
    "                max=value\n",
    "        print(\"样本中最大值是：%s\" % max)\n",
    "    if min:\n",
    "        for value in arg:\n",
    "            min=value\n",
    "        for value in arg:\n",
    "            if value<min:\n",
    "                min=value\n",
    "        print(\"样本中最小值是：%s\" % min)\n",
    "    if meanerror:\n",
    "        for value in arg:\n",
    "            count+=1\n",
    "            sum+=value\n",
    "        ave=sum/count\n",
    "        sumsquare=0\n",
    "        for value in arg:\n",
    "            sumsquare+=value**2\n",
    "        var=sumsquare/count-ave**2\n",
    "        samplevar=var*count/(count-1)#将有偏的方差修正成无偏方差#\n",
    "        nsquare=math.sqrt(count)\n",
    "        print(\"均值标准误为：%s\" % (samplevar/nsquare))  \n",
    "statistics(1,1,1,2,2,3,4,5)\n",
    "print(\"\\n\")\n",
    "statistics(0,1,2,5,11,9,6,7)\n",
    "statistics(0,0,0,4,6,8)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
